﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.ConfigForm.Children
{
    public partial class Colors : Krilib.Control.ConfigPage
    {
        public Colors()
        {
            InitializeComponent();
        }

        private void Colors_Load(object sender, EventArgs e)
        {
            try
            {
                //Base
                AddColor(Lang.ColorConfig.ForeColor,  Core.Config.Appearance.BaseColorConfig.ForeColor);
                AddColor(Lang.ColorConfig.ForeSecondaryColor, Core.Config.Appearance.BaseColorConfig.ForeSecondaryColor);
                AddColor(Lang.ColorConfig.LinkTextColor, Core.Config.Appearance.BaseColorConfig.LinkTextColor);
                AddColor(Lang.ColorConfig.ListBackgroundColor, Core.Config.Appearance.BaseColorConfig.ListBackgroundColor);

                //Additional
                AddColor(Lang.ColorConfig.ListAlternateBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListAlternateBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListAlternateBackgroundColor.Enabled);

                AddColor(Lang.ColorConfig.ListNewestBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListNewestBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListNewestBackgroundColor.Enabled);
                AddColor(Lang.ColorConfig.ListReplyToMeBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListReplyToMeBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListReplyToMeBackgroundColor.Enabled);
                AddColor(Lang.ColorConfig.ListReplyToMeNewestBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListReplyToMeNewestBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListReplyToMeNewestBackgroundColor.Enabled);



                AddColor(Lang.ColorConfig.ListReplyTextColor, Core.Config.Appearance.Renderer.Colors.ListReplyTextColor);
                AddColor(Lang.ColorConfig.ListReplyNotfoundTextColor, Core.Config.Appearance.Renderer.Colors.ListReplyNotfoundTextColor);
                AddColor(Lang.ColorConfig.ListReplyBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListReplyBackgroundColor);

                AddColor(Lang.ColorConfig.ListHeaderFooterBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListHeaderFooterBackgroundColor);
                AddColor(Lang.ColorConfig.ListHeaderFooterTextColor, Core.Config.Appearance.Renderer.Colors.ListHeaderFooterTextColor);

                AddColor(Lang.ColorConfig.ListMyPostBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListMyPostBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListMyPostBackgroundColor.Enabled);

                AddColor(Lang.ColorConfig.ListReplyFromSelectedBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListReplyFromSelectedBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListReplyFromSelectedBackgroundColor.Enabled);
                AddColor(Lang.ColorConfig.ListReplyToSelectedBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListReplyToSelectedBackgroundColor,
                   Core.Config.Appearance.Renderer.Colors.ListReplyToSelectedBackgroundColor.Enabled);
                AddColor(Lang.ColorConfig.ListSelectedUserBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListSelectedUserBackgroundColor,
                    Core.Config.Appearance.Renderer.Colors.ListSelectedUserBackgroundColor.Enabled);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public override void Apply()
        {
            foreach (var l in cdList)
            {
                l.Reflect();
            }
            base.Apply();
        }

        private class ColorData
        {
            public string name = null;
            public Color value = Color.Transparent;
            public Krile.Imported.Data.XColor reflectTo = new Krile.Imported.Data.XColor();
            public bool enabled = false;
            public bool disablable = false;
            public void DoConfig(DataGridViewButtonCell bc)
            {
                if (bc == null)
                    throw new ArgumentNullException();
                using (var cd = new ColorDialog())
                {
                    cd.AllowFullOpen = true;
                    cd.AnyColor = true;
                    cd.Color = value;
                    cd.FullOpen = true;
                    cd.SolidColorOnly = false;
                    if (cd.ShowDialog() == DialogResult.OK)
                    {
                        value = cd.Color;
                        bc.Style.BackColor = value;
                        bc.Style.SelectionBackColor = value;
                    }
                }
            }

            public void Reflect()
            {
                reflectTo.Value = value;
                if (disablable)
                {
                    ((Data.SXColor)reflectTo).Enabled = enabled;
                }
            }
        }

        List<ColorData> cdList = new List<ColorData>();
        private void AddColor(string name, Krile.Imported.Data.XColor xclr)
        {
            AddColor(name, xclr, true, false);
        }

        private void AddColor(string name, Krile.Imported.Data.XColor xclr, bool enabled)
        {
            AddColor(name, xclr, enabled, true);
        }

        private void AddColor(string name, Krile.Imported.Data.XColor xclr, bool enabled, bool disablable)
        {
            var cd = new ColorData() { name = name, value = xclr.Value, disablable= disablable, enabled = enabled, reflectTo = xclr };
            cdList.Add(cd);
            var dgvr = new DataGridViewRow();
            var text = new DataGridViewTextBoxCell();
            text.Value = name;
            text.Style.WrapMode = DataGridViewTriState.True;
            dgvr.Cells.Add(text);
            var color = new DataGridViewButtonCell();
            color.FlatStyle = FlatStyle.Flat;
            dgvr.Cells.Add(color);
            if (disablable)
            {
                var chk = new DataGridViewCheckBoxCell();
                chk.Value = enabled;
                dgvr.Cells.Add(chk);
                chk.Tag = cd;
            }
            else
            {
                var chk = new DataGridViewTextBoxCell();
                dgvr.Cells.Add(chk);
                chk.ReadOnly = true;
                chk.Tag = cd;
            }
            color.Tag = cd;
            color.Style.BackColor = xclr.Value;
            color.Style.SelectionBackColor = xclr.Value;
            ColorSelector.Rows.Add(dgvr);
        }

        private void ColorSelector_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 1)
                {
                    ((ColorData)ColorSelector[e.ColumnIndex, e.RowIndex].Tag).DoConfig(ColorSelector[e.ColumnIndex, e.RowIndex] as DataGridViewButtonCell);
                }
                else if (e.ColumnIndex == 2)
                {
                    if (!((ColorData)ColorSelector[e.ColumnIndex, e.RowIndex].Tag).disablable)
                        return;

                    ((ColorData)ColorSelector[e.ColumnIndex, e.RowIndex].Tag).enabled =
                        !((ColorData)ColorSelector[e.ColumnIndex, e.RowIndex].Tag).enabled;
                    ColorSelector[e.ColumnIndex, e.RowIndex].Value = ((ColorData)ColorSelector[e.ColumnIndex, e.RowIndex].Tag).enabled;
                    ColorSelector.Refresh();
                }
            }
        }
    }
}
